﻿using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;


namespace WpfApp8._0
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private PluginLoadContext _loadContext;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void LoadPluginButton_Click(object sender, RoutedEventArgs e)
        {
            var pluginPath = "D:\\Code\\FirstCode\\FirstCode\\Normal\\Assembly\\WpfApp8.0\\bin\\Debug\\net8.0-windows";
            //_loadContext = new PluginLoadContext(pluginPath);

            //try
            //{
            //    var assembly = _loadContext.LoadPlugin();
            //    // 使用加载的程序集...
            //    assembly.GetType("MyMathLibrary.MathOperations");
            //    var instance = Activator.CreateInstance(assembly.GetType("MyMathLibrary.MathOperations"));
            //    var result = instance.GetType().InvokeMember("Add", System.Reflection.BindingFlags.InvokeMethod, null, instance, new object[] { 5, 3 });
            //    MessageBox.Show($"插件已成功加载{result}！");
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show($"加载插件时出错: {ex.Message}");
            //}
            // 创建新的 AppDomain
            var domainSetup = new AppDomainSetup();
                domainSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
                var tempDomain = AppDomain.CreateDomain("TempDomain", null, domainSetup);

                // 加载并使用 DLL
                var assemblyPath = "path/to/your/dll.dll";
                var assembly = Assembly.LoadFrom(assemblyPath);
            //使用加载的程序集...
                assembly.GetType("MyMathLibrary.MathOperations");
                var instance = Activator.CreateInstance(assembly.GetType("MyMathLibrary.MathOperations"));
                var result = instance.GetType().InvokeMember("Add", System.Reflection.BindingFlags.InvokeMethod, null, instance, new object[] { 5, 3 });
                MessageBox.Show($"插件已成功加载{result}！");
        }

        private void UnloadPluginButton_Click(object sender, RoutedEventArgs e)
        {
            if (_loadContext != null)
            {
                _loadContext.Unload();

                // 等待垃圾回收以确保资源被释放
                GC.Collect();
                GC.WaitForPendingFinalizers();

                // 现在可以安全地删除 DLL 文件
                string dllPath = Path.Combine("D:\\Code\\FirstCode\\FirstCode\\Normal\\Assembly\\WpfApp8.0\\bin\\Debug\\net8.0-windows","WpfLibrary1.dll");
                if (File.Exists(dllPath))
                {
                    File.Delete(dllPath);
                    MessageBox.Show("插件已成功卸载并删除！");
                }
                else
                {
                    MessageBox.Show("插件文件不存在。");
                }
            }
            else
            {
                MessageBox.Show("没有加载的插件。");
            }
        }
    }
}